package com.example.gitclient.adapter;

import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.gitclient.R;
import com.example.gitclient.model.Commit;

import java.util.ArrayList;
import java.util.List;

/**
 * 提交历史列表适配器
 */
public class CommitAdapter extends RecyclerView.Adapter<CommitAdapter.ViewHolder> {
    private final List<Commit> commits;

    /**
     * 创建提交历史列表适配器
     * @param commits 提交列表
     */
    public CommitAdapter(List<Commit> commits) {
        this.commits = commits != null ? commits : new ArrayList<>();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_commit, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Commit commit = commits.get(position);
        holder.tvCommitId.setText(commit.getShortId());
        holder.tvCommitMessage.setText(commit.getMessage());
        holder.tvAuthor.setText(commit.getAuthor());
        holder.tvDate.setText(DateFormat.format("yyyy-MM-dd HH:mm", commit.getDate()));
    }

    @Override
    public int getItemCount() {
        return commits.size();
    }

    /**
     * 视图持有者
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvCommitId;
        TextView tvCommitMessage;
        TextView tvAuthor;
        TextView tvDate;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvCommitId = itemView.findViewById(R.id.tvCommitId);
            tvCommitMessage = itemView.findViewById(R.id.tvCommitMessage);
            tvAuthor = itemView.findViewById(R.id.tvAuthor);
            tvDate = itemView.findViewById(R.id.tvDate);
        }
    }
}